package linkedlist;

import java.util.LinkedList;
import java.util.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/3/25
 * Time: 11:31
 */
public class SingleLinkedListDemo {

    public static void main(String[] args) {

        /*HeroNode hero1 = new HeroNode(1,"宋江","及时雨");        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(3,"吴用","智多星");
        HeroNode hero4 = new HeroNode(4,"林冲","豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();

//        singleLinkedList.add(hero1);
//        singleLinkedList.add(hero2);
//        singleLinkedList.add(hero3);
//        singleLinkedList.add(hero4);

        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero3);

//        System.out.println(getLength(singleLinkedList.getHerd()));
        singleLinkedList.list();*/
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
        HeroNode hero3 = new HeroNode(3,"吴用","智多星");
        HeroNode hero5 = new HeroNode(5,"关胜","大刀");
        HeroNode hero7 = new HeroNode(7,"秦明","霹雳火");
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero5);
        singleLinkedList.addByOrder(hero7);
        //---------------------------------------
        SingleLinkedList singleLinkedList2 = new SingleLinkedList();
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero4 = new HeroNode(4,"公孙胜","入云龙");
        HeroNode hero6 = new HeroNode(6,"林冲","豹子头");
        HeroNode hero8 = new HeroNode(8,"呼延灼","双鞭");
        singleLinkedList2.addByOrder(hero2);
        singleLinkedList2.addByOrder(hero4);
        singleLinkedList2.addByOrder(hero6);
        singleLinkedList2.addByOrder(hero8);
        SingleLinkedList singleLinkedList3 = new SingleLinkedList();
        mergeList(singleLinkedList.getHerd(),singleLinkedList2.getHerd());
        singleLinkedList2.list();
        System.out.println("----");
        singleLinkedList.list();

       /* reverseList(singleLinkedList.getHerd());
        System.out.println("-----倒序之后-------");
        singleLinkedList.list();*/
    }
    public static int getLength(HeroNode head) {
        int len = 0;
        HeroNode cur = head.next;
        while (true) {
            if(cur == null) {
                break;
            }
            len++;
            cur = cur.next;
        }
        return len;
    }
    /**
     * 查倒数第X个节点
     * @param herd
     * @param index 到数第几个
     * @return
     */
    public static HeroNode findLastIndexNode(HeroNode herd, int index) {
        //判断如果链表为空，返回null
        if (herd.next == null) {
            return null;
        }
        int size = getLength(herd);//节点个数
        if(index <= 0 || index > size) {
            return null;
        }
        HeroNode cur = herd.next;
        for(int i = 1;i<= size-index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    /*
     *  将单链表反转
     */
    public static void reverseList(HeroNode  head) {
        if(head.next == null || head.next == null) {
            return;
        }
        HeroNode cur = head.next; //第一个有效节点()
        HeroNode next = null; // 指向当前节点(cur)的下一个节点
        HeroNode reverseHead = new HeroNode(0, "", "");
        //遍历原来的链表，每遍历一个节点，就将其取出并放在新的链表reverseHead 的最强端
        while(cur != null) {
            next = cur.next;
            cur.next = reverseHead.next; // 将cur指向reverseHead的最前端(有效数据的最前面)
            reverseHead.next = cur;
            cur = next;
        }
        head.next = reverseHead.next;

    }
    /**
     * 逆序打印
     */
    public static void reversePrint(HeroNode head) {
        if(head.next == null) {
            return;
        }
        Stack<HeroNode> stack = new Stack<HeroNode>();
        HeroNode cur = head.next;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }

    public static HeroNode mergeList(HeroNode head1, HeroNode head2) {
        HeroNode temp1 = head1.next;
        HeroNode temp2 = head2;
        if(temp1 == null && temp2.next != null) {
            return temp2;
        }else if(temp2.next == null && temp1 != null){
            return temp1;
        }else if(temp2.next == null && temp1 == null){
            return null;
        }
        while (temp1 !=  null) {
            HeroNode temp3 = temp1.next;
            while (temp2 != null) {
                if(temp2.next == null) {
                    break;
                }
                if(temp2.next.no > temp1.no) {
                    break;
                }
                temp2 = temp2.next;
            }
            temp1.next = temp2.next;
            temp2.next = temp1;

            temp1 = temp3;
        }
        return  temp2;
    }

}
/**
 * 定义SingleLinkedList
 */
class SingleLinkedList {
    //先初始化一个头节点，头结点不要动，不存放具体的数据
    private HeroNode herd = new HeroNode(0, "", "");

    public HeroNode getHerd() {
        return herd;
    }

    public void setHerd(HeroNode herd) {
        this.herd = herd;
    }

    //添加节点到单向链表
    public void add(HeroNode herdNode) {
        //因为head节点不能动，因此需要一个辅助遍历temp
        HeroNode temp = herd;
        //遍历链表，找到最后
        while (true) {
            //找到链表的最后
            if(temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //当退出while循环的时，temp就指向了链表的最后
        temp.next = herdNode;
//        System.err.println(temp+""+herd);
    }

    //根据排名(no)将英雄(HeroNode)插入到指定位置
    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = herd;
        boolean flag = false; // flag标记添加的编号是否存在
        while(true) {
            if(temp.next == null) {
                break;
            }
            if(temp.next.no > heroNode.no) {
                break;
            }else if(temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            System.out.println("编号" + heroNode.no + "的英雄已存在，无法加入");
        }else {
            //插入到链表中，temp的后面
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    public void update(HeroNode newHeroNode) {
        //判断是否空
        if(herd.next == null) {
            System.err.println("链表为空");
            return;
        }
        HeroNode temp = herd.next;
        boolean flag = false;
        while (true) {
            if(temp == null) {
                break;
            }
            if(temp.no == newHeroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag判断是否找到要修改的节点
        if(flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else {
            System.out.printf("没有找到编号%d的节点。不能修改\n", newHeroNode.no);
        }
    }

    public void del(int no) {
        HeroNode temp = herd;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if(temp.next.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
            if(flag) {
                temp.next = temp.next.next;
            }else {
                System.err.printf("要删除的编号为%d节点不存在",no);
            }
        }
    }

    //显示链表[遍历]
    public void list() {
        //判断链表是否为空
        if(herd.next == null) {
            System.err.println("链表为空");
            return;
        }
        //因为头节点不能动，需要一个辅助变量
        HeroNode temp =  herd.next;
        while (true) {
            //输出节点的信息
            System.out.println(temp);
            //判断是否链表到最后
            if(temp.next == null) {
                break;
            }
            //将temp后移
            temp = temp.next;
        }

    }

}

/**
 * 定义HeroNode， 每个HeroNode 对象就是一个节点
  */
class HeroNode {
    public int no;
    public String name;
    public String nickname;

    public HeroNode next; //指向下一个节点
    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //重新toString

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname  + '\''+
                '}';
    }
}


